﻿//-------------------Tooltip-------------------
function Tooltip(x) {
    $(x).tooltip({ toggle: 'tooltip'});
};
function EditRows(x) {
    x.find('.editclick').click();
};
//--------------------Popup--------------------
var _confirm = false;
function Show(pop) {
    $(pop + ' input').each(function () {
        if ($(this).data('allow') == 'add') {
            document.getElementById($(this).attr('id')).disabled = false;
        }
        if (!$(this).hasClass("textbox_child_text_ngay")) {
            $(this).val('');
        }
    });
    $(pop + ' select').each(function () {
        $(this).find('option:selected').prop('selected', false);
        $(this).find('option:eq(0)').prop('selected', true);
    });
    $(pop + ' textarea').each(function () {
        $(this).val('');
    });
    $(pop).dialog("open");
    return false;
}
function Hide(pop) {
    $(pop).dialog("close");
    return false;
}
function PopupUpdate(pop) {
    $(pop).dialog({
        autoOpen: false,
        modal: true,
        draggable: true,
        height: "auto",
        width: "auto",
        resizable: false,
        create: function (event) { $(event.target).parent().css('position', 'fixed'); },
        open: function () {
            $(this).parent().appendTo("form");
        }
    });
}
function PopupUpdateHeight(pop) {
    $(pop).dialog({
        autoOpen: false,
        modal: true,
        draggable: true,
        height: "auto",
        width: "1002",
        resizable: false,
        create: function (event) { $(event.target).parent().css('position', 'fixed'); },
        open: function () {
            $(this).parent().appendTo("form");
        }
    });
}
$(document).ready(function () {
    $('#thongbao').dialog({
        autoOpen: false,
        draggable: true,
        height: "auto",
        width: "372px",
        resizable: false,
        create: function (event) { $(event.target).parent().css('position', 'fixed'); },
        open: function () {
            $(this).parent().appendTo("form");
        }
    });
    return false;
});
function confirmCheckIn(button) {
    if (_confirm == false) {
        jQuery('<div>')
            .html("<p>Bạn có muốn xoá mẫu tin này không ? </p>")
            .dialog({
                autoOpen: true,
                modal: true,
                title: "Thông Báo",
                height: "auto",
                width: "372px",
                buttons: {
                    "Đồng ý": function () {
                        jQuery(this).dialog("close");
                        _confirm = true;
                        button.click();
                        _confirm = false;
                    },
                    "Bỏ qua": function () {
                        jQuery(this).dialog("close");
                    }
                },
            });
    }
    return _confirm;
}
function confirmCheckInAdd(button) {
    if (_confirm == false) {
        jQuery('<div>')
            .html("<p>Chấp nhận ? </p>")
            .dialog({
                dialogClass: 'customClass',
                autoOpen: true,
                modal: true,
                title: "Thông Báo",
                height: "auto",
                width: "372px",
                buttons: {
                    "Đồng ý": function () {
                        jQuery(this).dialog("close");
                        jQuery($("#baogia_themsanpham")).dialog("close");
                        _confirm = true;
                        button.click();
                        _confirm = false;
                    },
                    "Bỏ qua": function () {
                        jQuery(this).dialog("close");
                    }
                },
            });
    }
    return _confirm;
}

//--------------------DatePicker--------------------
function DatePicker(id) {
    $(id).datepicker({
        showOn: "button",
        buttonImage: "../Images/calendar.gif",
        dateFormat: 'dd/mm/yy',
        changeMonth: true,
        changeYear: true,
        buttonImageOnly: true
    });
    return false;
}
function DateTimePicker(id) {
    $(id).datetimepicker({
        showOn: "button",
        buttonImage: "../Images/calendar.gif",
        changeMonth: true,
        changeYear: true,
        buttonImageOnly: true
    });
    return false;
}
function MarkInputDay(id) {
    $(id).mask("99/99/9999"); //Kiem tra ngay nhap
    return false;
}
function MarkInputDayTime(id) {
    $(id).mask("99/99/9999 23:59"); //Kiem tra ngay nhap
    return false;
}
function MarkInputPhone10(id) {
    $(id).mask("(999) 999-9999");//Kiem tra so dien thoai 10 so
    return false;
}
function MarkInputPhone11(id) {
    $(id).mask("(9999) 999-999");//Kiem tra so dien thoai 10 so
    return false;
}
//------------------TabletSort-----------------
function TableSort(tableId,pagerclass) {
    $(document).ready(function () {
        var pagerOptions = {
            container: $(pagerclass),
            ajaxUrl: null,
            customAjaxUrl: function(table, url) { return url; },
            ajaxProcessing: function(ajax){
                if (ajax && ajax.hasOwnProperty('data')) {
                    return [ ajax.total_rows, ajax.data ];
                }
            },
            output: '{startRow} - {endRow} / ({totalRows})',
            updateArrows: true,
            page: 0,
            size: 10,
            savePages : true,
            storageKey:'tablesorter-pager',
            fixedHeight: true,
            removeRows: false,
            cssNext: '.next', // next page arrow
            cssPrev: '.prev', // previous page arrow
            cssFirst: '.first', // go to first page arrow
            cssLast: '.last', // go to last page arrow
            cssGoto: '.gotoPage', // select dropdown to allow choosing a page

            cssPageDisplay: '.pagedisplay', // location of where the "output" is displayed
            cssPageSize: '.pagesize', // page size selector - select dropdown that sets the "size" option

            cssDisabled: 'disabled', // Note there is no period "." in front of this class name
            cssErrorRow: 'tablesorter-errorRow' // ajax error information row
        };
        $(tableId).tablesorter({
            theme: "blue",
            headerTemplate: '{content} {icon}',
            widgets: ["zebra", "resizable"],
            widgetOptions: {
                zebra: ["even", "odd"],
                filter_reset: ".reset",
                resizable: true
            },
            sortReset: true,
            sortRestart: true
        })
            .bind('pagerChange pagerComplete pagerInitialized pageMoved', function (e, c) {
                var msg = '"</span> event triggered, ' + (e.type === 'pagerChange' ? 'going to' : 'now on') +
                  ' page <span class="typ">' + (c.page + 1) + '/' + c.totalPages + '</span>';
                $('#display')
                  .append('<li><span class="str">"' + e.type + msg + '</li>')
                  .find('li:first').remove();
            })
            .tablesorterPager(pagerOptions
                //container: $(".ts-pager"),
                //cssGoto: ".pagenum",
                //removeRows: false,
                //output: '{startRow} - {endRow} / {filteredRows} ({totalRows})'
            );
    });
    return false;
}
//------------------CheckAll-----------------
function checkAll() {
    $('.tablesorter tbody tr').each(function () {
        if ($('input#chkAll').is(":checked")) {
            $('input#chkList').prop({ 'checked': 'checked' });
            $('input#chkList').parent().parent().addClass('warning');
        } else {
            $('input#chkList').prop({ 'checked': false });
            $('input#chkList').parent().parent().removeClass('warning');
        }
    });
}
function checkAllQuotes() {
    $('.tablesorter tbody tr').each(function () {
        if ($('input#chkAllQuotes').is(":checked")) {
            $('input#chkListQuotes').prop({ 'checked': 'checked' });
            $('input#chkListQuotes').parent().parent().addClass('warning');
        } else {
            $('input#chkListQuotes').prop({ 'checked': false });
            $('input#chkListQuotes').parent().parent().removeClass('warning');
        }
    });
}
//------------------FormatNum-----------------
function FormatNumber(str) {
    var strTemp = GetNumber(str);
    if (strTemp.length <= 3)
        return strTemp;
    var strResult = "";
    var i;
    for (i = 0; i < strTemp.length; i++)
        strTemp = strTemp.replace(",", "");
    for (i = strTemp.length; i >= 0; i--) {
        if (strResult.length > 0 && (strTemp.length - i - 1) % 3 == 0)
            strResult = "," + strResult;
        strResult = strTemp.substring(i, i + 1) + strResult;
    }
    return strResult;
}
function GetNumber(str) {
    for (var i = 0; i < str.length; i++) {
        var temp = str.substring(i, i + 1);
        if (!(temp == "," || temp == "." || (temp >= 0 && temp <= 9))) {
            alert("Vui lòng nhập số (0-9)!");
            //jAlert('Vui lòng nhập số (0-9) !', "Thông báo");
            return str.substring(0, i);
        }
        if (temp == " ")
            return str.substring(0, i);
    }
    return str;
}

function TongSoKhac(str) {
    var strTemp = GetNumber(str);

    return strTemp;
}
function LengthComment(str) {
    $("#charLeft").text(str.length + ' char');
}

function ValidateKeypress(numcheck, e) {
    var keynum, keychar;
    if (window.event) {//IE
        keynum = e.keyCode;
    }
    else if (e.which) {// Netscape/Firefox/Opera
        keynum = e.which;
    }
    if (keynum == 8 || keynum == 127 || keynum == null || keynum == 9 || keynum == 0 || keynum == 13) return true;
    keychar = String.fromCharCode(keynum);
    var result = numcheck.test(keychar);
    return result;
}
//-------------------------Check Save----------------------
function updateTips(t) {
    var tips = $(".validateTips");
    tips
        .text(t)
        .addClass("ui-state-highlight");
    setTimeout(function () {
        tips.removeClass("ui-state-highlight", 5500);
    }, 4500);
}
function checkLength(o, n, min, max) {
    if (o.val().length < min) {
        o.addClass("ui-state-error");
        updateTips("« " + n + " không được trống »");
        return false;
    }
    else
        if (o.val().length > max) {
            updateTips("« " + n + " phải nhỏ hơn " + max + " kí tự »");
            return false;
        }
        else {
            return true;
        }
}
function checkLengthAndNumber(o, n, min, max) {
    if (o.val().length > max || o.val().length < min) {
        o.addClass("ui-state-error");
        updateTips("« " + n + " không được trống và phải là số »");
        return false;
    } else {
        return true;
    }
}
function checkdr(o, n) {
    //alert(o.val());                   value Dropdownlist
    if (o.prop('selectedIndex') == 0) {
        o.addClass("ui-state-error");
        updateTips("« Vui lòng chọn " + n + " »");
        return false;
    } else {
        return true;
    }
}
function checkRegexp(o, regexp, n) {
    if (!(regexp.test(o.val()))) {
        o.addClass("ui-state-error");
        updateTips(n);
        return false;
    } else {
        return true;
    }
}
//----------------------Tabs----------------------
function addTab(tab) {
    $(".tabs-title li a[href='" + tab + "']").click().parent().removeClass("removetabs").addClass("opentabs");
    $(tab).removeClass("removetabs").addClass("opentabs");
}
function removeTab(tab) {    
    $(".tabs-title li a[href='" + tab + "']").parent().removeClass("opentabs").addClass("removetabs");
    $(tab).removeClass("opentabs").addClass("removetabs");
    //$(".tabs-title li:eq(0) a").click();
    $(".tabs-title li a[href!='" + tab + "']").each(function () {
        if (!$(this).parent().hasClass('ui-tabs-active')) {
            $(this).click();
            return false;
        }
    });
}
$(function () {
    $('.tabs-title li .close').click(function () {
        removeTab($(this).parent().find('.ui-tabs-anchor').attr('href'));
    })
});
//function HeightResize()
//{
//    var h = $("#sidebar-wrapper").height();
//    //alert(h);
//    $("#sidebar-wrapper #MainMenu a.list-group-item").each(function () {
//        h = h - $(this).height() - 14.5;
//    });
//    $(".heightmenu").height(h);
//}

//-------------------Dropdown------------------------
function setSelectedValue(selectObj, valueToSet) {
    for (var i = 0; i < selectObj.options.length; i++) {
        if (selectObj.options[i].text == valueToSet) {
            selectObj.options[i].selected = true;
            return;
        }
    }
};
//-------------------End------------------------